package com.laf.manage.controller.admin;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.laf.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.laf.common.annotation.Log;
import com.laf.common.core.controller.BaseController;
import com.laf.common.core.domain.AjaxResult;
import com.laf.common.enums.BusinessType;
import com.laf.manage.domain.po.Tag;
import com.laf.manage.service.ITagService;
import com.laf.common.utils.poi.ExcelUtil;
import com.laf.common.core.page.TableDataInfo;

/**
 * 标签Controller
 *
 * @author zian
 * @date 2024-11-09
 */
@Api(tags="标签管理")
@RestController("adminTagController")
@RequestMapping("/admin/tag")
public class TagController extends BaseController
{
    @Autowired
    private ITagService tagService;

    /**
     * 查询标签列表
     */
    @ApiOperation("查询标签列表")
    @PreAuthorize("@ss.hasPermi('manage:tag:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tag tag)
    {
        startPage();
        List<Tag> list = tagService.selectTagList(tag);
        return getDataTable(list);
    }

    /**
     * 导出标签列表
     */
    @ApiOperation("导出标签列表")
    @PreAuthorize("@ss.hasPermi('manage:tag:export')")
    @Log(title = "标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tag tag)
    {
        List<Tag> list = tagService.selectTagList(tag);
        ExcelUtil<Tag> util = new ExcelUtil<Tag>(Tag.class);
        util.exportExcel(response, list, "标签数据");
    }

    /**
     * 获取标签详细信息
     */
    @ApiOperation("获取标签详细信息")
    @PreAuthorize("@ss.hasPermi('manage:tag:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tagService.getById(id));
    }

    /**
     * 新增标签
     */
    @ApiOperation("新增标签")
    @PreAuthorize("@ss.hasPermi('manage:tag:add')")
    @Log(title = "标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tag tag)
    {
        LocalDateTime now = LocalDateTime.now();
        String username = SecurityUtils.getUsername();
        tag.setCreateTime(now);
        tag.setUpdateTime(now);
        tag.setCreateBy(username);
        tag.setUpdateBy(username);
        return toAjax(tagService.save(tag));
    }

    /**
     * 修改标签
     */
    @ApiOperation("修改标签")
    @PreAuthorize("@ss.hasPermi('manage:tag:edit')")
    @Log(title = "标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tag tag)
    {
        tag.setUpdateTime(LocalDateTime.now());
        tag.setUpdateBy( SecurityUtils.getUsername());
        return toAjax(tagService.updateById(tag));
    }

    /**
     * 删除标签
     */
    @ApiOperation("删除标签")
    @PreAuthorize("@ss.hasPermi('manage:tag:remove')")
    @Log(title = "标签", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tagService.removeByIds(Arrays.asList(ids)));
    }
}
